import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Tsarev Oleg<br/>
 *		   <b>mail:</b> tsarev.oi@mail.ru<br/>
 *         <b>date:</b> 12.07.2010<br/>
 *         <b>time:</b> 14:04:06
 *
 */
public class Importer {

	public static void main(String[] args) throws IOException {
		if (args.length<2) {
			throw new RuntimeException("Not enought parameters...");
		}
		
		File source = new File(args[0]);
		File target = new File(args[1]);
		
		if (source.exists()) {
			target.createNewFile();
			BufferedReader fr = new BufferedReader(new FileReader(source));
			BufferedWriter fw = new BufferedWriter(new FileWriter(target));
			
			String line;
			while ((line=fr.readLine())!=null) {
				
				if (line.trim().contains("/*import src=")) {
					
					String[] importFilename = line.split("\"");
					if (importFilename.length==3) {
						
						System.out.println(importFilename[1]);
						File importedFile = new File(importFilename[1]);
						
						if (importedFile.exists()) {
							BufferedReader ifr = new BufferedReader(new FileReader(importedFile));
							String iline;
							
							while ((iline=ifr.readLine())!=null) {
								fw.write(iline);
								fw.newLine();
							}
							
							ifr.close();
						} else {
							throw new RuntimeException("Source file conteins bad import instruction...");
						}
						
					} else {
						throw new RuntimeException("Source file conteins bad import instruction...");
					}
					
				} else {
					fw.write(line);
					fw.newLine();
				}
			}
			 
			fw.flush();
			fw.close();
			fr.close();
			
		} else {
			throw new RuntimeException("Source file not exists...");
		}

	}

}
